 #curl -k -s -o /tmp/oray.log "http://yzqiang:yzqyzq1234@ddns.oray.com/ph/update?hostname=smccb.51vip.biz,yzqiang.iask.in" &
 #curl -k -s -o /tmp/3322.log "http://yzqiang6:yzqyzq1234@members.3322.net/dyndns/update?hostname=yzqiang6.f3322.net" &

cd /tmp
echo "begin update freenom" >>/tmp/freenom.log
current_ip=`curl -kLs https://ip.3322.net/`


#echo $current_ip >/tmp/freenom.log
#source config
freenom_email="yzqiang5@gmail.com"
freenom_passwd="yzqyzq"
freenom_domain_name="yzqiang.tk"
freenom_domain_id="1059939135"

#freenom_domain_name="smccb.tk"
#freenom_domain_id="1059941577"

BASE_URL="https://my.freenom.com"
CAREA_URL="$BASE_URL/clientarea.php"
LOGIN_URL="$BASE_URL/dologin.php"
LOGOUT_URL="$BASE_URL/logout.php"
MANAGED_URL="$CAREA_URL?managedns=$freenom_domain_name&domainid=$freenom_domain_id"



resolveip=`/usr/bin/resolveip -4 -t 4 $freenom_domain_name | grep -v : | sed -n '1p'`
if [ ! -n "$resolveip" ] ; then
TMP=`ping -c 1 -4 $freenom_domain_name | grep "PING "`
OO=`echo "$TMP"|awk -F '(' '{print $2}'`
OO=`echo "$OO"|awk -F ')' '{print $1}'`
resolveip=$OO
fi


if [ "$current_ip"X == "$resolveip"X ] ; then
    echo "$current_ip"  not modified. >>/tmp/freenom.log
    echo "$current_ip"  not modified.
    logger "$current_ip"  not modified.	
    exit 1
else
    echo try update "$resolveip" to "$current_ip"	>>/tmp/freenom.log
	echo try update "$resolveip" to "$current_ip"	
	logger try update "$resolveip" to "$current_ip"	
fi

rm /tmp/c.txt 2>/dev/null
rm /tmp/freenom.html 2>/dev/null
curl  -kL -o /tmp/freenom.html -c /tmp/c.txt -F "username=$freenom_email" -F "password=$freenom_passwd" -e "$CAREA_URL"    "$LOGIN_URL"  2>/dev/null
if [ ! -s /tmp/c.txt ]; then
    echo  "Login failed: empty cookie."	>>/opt/tmp/freenom.log
    echo "Login failed: empty cookie."
    logger "Login failed: empty cookie."
    exit 1
fi

if grep "/clientarea.php?incorrect=true" /tmp/freenom.html  ; then
    echo "Login failed."	>>/tmp/freenom.log
    echo "Login failed."
    logger "Login failed."
    exit 1
fi

rm /tmp/freenom.html 2>/dev/null
curl  -kL -o /tmp/freenom.html -b /tmp/c.txt -e "$MANAGED_URL" -F "dnsaction=modify" \
 -F "records[0][line]=" -F "records[0][type]=A" -F "records[0][name]="    -F "records[0][ttl]=600" -F "records[0][value]=$current_ip" \
 -F "records[1][line]=" -F "records[1][type]=A" -F "records[1][name]=www" -F "records[1][ttl]=600" -F "records[1][value]=$current_ip" \
 "$MANAGED_URL"  2>/dev/null
 
 
if [  -s /tmp/freenom.html ]; then 
if grep -q "/clientarea.php?incorrect=true" /tmp/freenom.html  >/dev/null ; then
    echo"Update failed."	>>/opt/tmp/freenom.log
    echo "Update failed."
    logger "Update failed."
fi

    echo "$resolveip" Update to "$current_ip" finished.	>>/tmp/freenom.log
    echo "$resolveip" Update to "$current_ip" finished.
    logger "$resolveip" Update to "$current_ip" finished.
curl  -kL -o /dev/null -b /tmp/c.txt "$LOGOUT_URL" 2>/dev/null
exit 1


if grep -q "name=\"records\[0\]\[value\]\" value=\"$current_ip\"" /tmp/freenom.html >/dev/null ; then
    echo "$resolveip" Update to "$current_ip" sucess.	>>/tmp/freenom.log
    echo "$resolveip" Update to "$current_ip" sucess.
    logger "$resolveip" Update to "$current_ip" sucess.
else
    echo "Update failed."	>>/tmp/freenom.log
    echo "Update failed."
    logger "Update failed."
fi
else
    echo "Update failed."  >>/tmp/freenom.log
    echo "Update failed."
    logger "Update failed."
fi 
curl  -kL -o /dev/null -b /tmp/c.txt "$LOGOUT_URL" 2>/dev/null



